﻿namespace Framework.FluentTextStringBuilder.ExtensionCSharp
{
    public static class CSharpFtsbExtensions
    {
        /// <summary>
        /// Code block begin.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns></returns>
        /// <remarks>
        /// StartOfBlock
        /// {
        ///     ItemInBlock
        /// </remarks>
        public static ITextStringBuilderWriterFluent CodeBlock(this ITextStringBuilderWriterFluent builder)
        {
            builder
                .AddLine("{")
                .IndentRight();

            return builder;
        }
        /// <summary>
        /// Code block end
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <returns></returns>
        /// <remarks>
        ///     ItemInBlock
        /// }
        /// </remarks>
        public static ITextStringBuilderWriterFluent CodeBlock_(this ITextStringBuilderWriterFluent builder)
        {
            builder
                .IndentLeft()
                .AddLine("}");

            return builder;
        }
        /// <summary>
        /// Code block end (custom
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="codeBlockEndText">The code block end text.</param>
        /// <returns></returns>
        /// <remarks>
        ///     ItemInBlock
        /// };
        /// </remarks>
        public static ITextStringBuilderWriterFluent CodeBlock_(this ITextStringBuilderWriterFluent builder, string codeBlockEndText)
        {
            builder
                .IndentLeft()
                .AddLine(codeBlockEndText);

            return builder;
        }

        /// <summary>
        /// Add a block comment to the builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public static ITextStringBuilderWriterFluent Comment(this ITextStringBuilderWriterFluent builder, string comment)
        {
            builder
                .AddLine()
                .Add("// ").Divider()
                .AddLine("// {0}", comment)
                .Add("// ").Divider()
                .AddLine();
            
            return builder;
        }

        /// <summary>
        /// Add JSON Name/Value Block.
        /// NOTE: This may need to be in it's own Namespace for JSON
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="addSeperator">if set to <c>true</c> [add seperator].</param>
        /// <returns></returns>
        public static ITextStringBuilderWriterFluent NameValueJson(this ITextStringBuilderWriterFluent builder, string name, string value, bool addSeperator = true)
        {
            builder
                .IfAdd(addSeperator, ", ")
                .Add("{0}: '{1}'", FormatCodeUtil.ToIdentifier(name), value);

            return builder;
        }

        /// <summary>
        /// Add items with seperators the between items, e.g. Parmaters with commas
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder">The builder.</param>
        /// <param name="item">The item.</param>
        /// <param name="seperator">The seperator.</param>
        /// <returns></returns>
        public static ITextStringBuilderWriterFluent SeperatorBetweenItems<T>(this ITextStringBuilderWriterFluent builder, EnumerableExtension.NavigationItem<T> item, string seperator = ",")
        {
            if (!item.IsLast)
            {
                builder.Add(seperator);
            }
            
            return builder;
        }
    }
}